﻿using System;
using System.Data.SqlClient;
using System.Drawing;
using System.Net.NetworkInformation;
using System.Windows.Forms;
using Connection.MRIM;
using Connection.ORM;
using lgmkr;

namespace SmsSender
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
             _aClient = new Client();
            Message=new Sms();
            groupBox2.Enabled = false;
            
        }
        public Sms Message { get; set; }
        private User User { get; set; }

        private bool _state;
        private readonly Client _aClient;
        private LogMaker aMaker;

        bool InternetConnectionChecker()
        {
            IPStatus status=IPStatus.Unknown;
            try
            {
                status = new Ping().Send("google.com").Status;
            }
            catch (PingException e)
            {
                aMaker=new LogMaker {Error = e.Message};
                aMaker.PrintLog();
            }
            if (status != IPStatus.Success)
            {
                MessageBox.Show("Internet Connection Established");
                return false;
            }
            return true;
        }


        private void BtnSendClick(object sender, EventArgs e)
        {
            Message.Number = "+996" + mtxt_number.Text.Trim();
            Message.Text = txt_text.Text;
            Message.Date = DateTime.Now.ToShortDateString();
            Message.UserID = User.ID;

            if (_aClient.Logined)
                _aClient.SendSMS(Message.Number, Message.Text);
            else
            {
                lb_state.Text = "you Offline";
                lb_state.ForeColor = Color.Red;
                _state = false;
                groupBox2.Enabled = _state;
            }
            if (MessageBox.Show("Message sending successfully","Attention",MessageBoxButtons.OKCancel)==DialogResult.OK)
            {
                BtnClearClick(null, null);
            }
        }

        private void BtnClearClick(object sender, EventArgs e)
        {
            mtxt_number.Clear();
            txt_text.Clear();
            txt_text.Focus();
        }

        private void BtnConnectClick(object sender, EventArgs e)
        {
            if (InternetConnectionChecker())
            {
                User.Login = txt_login.Text.Trim();
                User.Domen = cmb_domen.Text;
                User.Password = txt_password.Text.Trim();
                if (_aClient.Login(User.Login + User.Domen, User.Password))
                {
                    lb_state.Text = "you Online";
                    lb_state.ForeColor = Color.YellowGreen;
                    _state = true;
                }
                else
                {
                    lb_state.Text = "you Offline";
                    lb_state.ForeColor = Color.Red;
                    _state = false;
                }
                groupBox2.Enabled = _state;
            }
        }

        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (_aClient.Logined)
                _aClient.LogOut();
            Application.Exit();
        }

        #region UserForm

        private void add_tmsi_user_Click(object sender, EventArgs e)
        {
            UserForm f=new UserForm(State.Add);
            f.ShowDialog();
        }

        private void edit_tmsi_user_Click(object sender, EventArgs e)
        {
            UserForm f = new UserForm(State.Edit);
            f.ShowDialog();
        }

        private void remove_tmsi_user_Click(object sender, EventArgs e)
        {
            UserForm f = new UserForm(State.Delete);
            f.ShowDialog();
        }

        #endregion

        private void add_tmsi_number_Click(object sender, EventArgs e)
        {
            NumberForm aNumberForm=new NumberForm(State.Add);
            aNumberForm.ShowDialog();
        }

        private void edit_tmsi_number_Click(object sender, EventArgs e)
        {
            NumberForm aNumberForm = new NumberForm(State.Edit);
            aNumberForm.ShowDialog();
        }

        private void remove_tmsi_number_Click(object sender, EventArgs e)
        {
            NumberForm aNumberForm = new NumberForm(State.Delete);
            aNumberForm.ShowDialog();
        }
    }
}
